package com.plant.serviceImpl;

import com.plant.dao.BaseDao;
import com.plant.entity.UgcCollect;
import com.plant.entity.UgcFaq;
import com.plant.entity.UgcFaqAnswer;
import com.plant.entity.UgcNewthings;
import com.plant.entity.UgcTopicJoin;
import com.plant.entity.UgcUploadImages;
import com.plant.entity.UgcUser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.plant.service.UserServic;

@Service    
@Transactional
public class UserInfoImlp implements UserServic{

	@Resource(name = "baseDaoImpl") 
	private BaseDao baseDao;

	@SuppressWarnings("unchecked")
	public List<Object> listUserInfo() {                                     //--1.查看用户的所有信息方法
		return baseDao.loadAll(UgcUser.class);
	}

	public List<Object> getByuid(UgcUser uf) { //--2通过用户id查找用户的信息
		String hql="from UgcUser ug where ug.id=?";
		return baseDao.find(hql, uf.getId());
	}

	public boolean updateU(UgcUser uf) {//--3.修改用户的基本资料实现方法
		return baseDao.update(uf);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> friendreq(UgcUser uf) {//--4.查看好友请求列表
		String hql="select A.id,B.nickname,B.photo,A.state,A.user_id from ugc_friends_apply as A ," +
				"ugc_user as B where A.user_id=B.id and A.to_user_id =("+uf.getId()+") ORDER BY create_time DESC LIMIT 0,1";
		return baseDao.Bysql(hql, UgcUser.class);
	}


	public Long myfriendcount(UgcUser uf) {
		String hql="select count(id) from UgcFriends where receiveUserId=("+uf.getId()+") or applyUserId=("+uf.getId()+")";
		long myfriendcount=baseDao.CountHql(hql);
		return myfriendcount;
	}


	@SuppressWarnings("unchecked")
	public List<Object> IsNotfriend(Integer uid,Integer rid) {//-----5.查询判断是否是好友
		List<Object> list=new ArrayList<Object>();
		String hql="select id from UgcFriends where receiveUserId=("+uid+") and applyUserId=("+rid+")";
		list=baseDao.find(hql);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object> isfriend(Integer uid,Integer rid) {//-----5.查询判断是否是好友
		List<Object> list=new ArrayList<Object>();
		String hql2="select id from UgcFriends where receiveUserId=("+rid+") and applyUserId=("+uid+")";
		list=baseDao.find(hql2);
		return list;
	}

	public Object OneUser(UgcUser uf) {  //---查看一个用户资料 返回一个对象
		return baseDao.find(UgcUser.class, uf.getId());
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> MyMoment(UgcUser uf,String time,Integer page) {//---个人动态
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="SELECT `id`,`user_id`,`create_time`,`content`,1 AS 'content_type' FROM ugc_newthings WHERE `user_id`=("+uf.getId()+")" +
				" AND `create_time`<'"+time+"' UNION SELECT `id`,`user_id`,`create_time`,`title`,3 AS 'content_type' FROM ugc_faq WHERE `user_id`=("+uf.getId()+") AND `create_time`<'"+time+"' " +
				"UNION SELECT `id`,`user_id`,`create_time`,`content`,5 AS 'content_type' FROM ugc_faq_answer WHERE `user_id`=("+uf.getId()+") " +
				"AND `create_time`<'"+time+"' UNION SELECT `id`,`user_id`,`create_time`,`content`,4 AS 'content_type' FROM ugc_topic_join WHERE `user_id`=("+uf.getId()+") " +
				"AND `create_time`<'"+time+"' ORDER BY `create_time` DESC LIMIT "+page+",10";
		list=baseDao.Bysql(hql, UgcUser.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> LanMuMyMoment(UgcUser uf,String time,Integer page) {//---个人动态
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		String hql="SELECT `id` id_key,`user_id`,`create_time`,`content`,1 AS 'content_type' FROM ugc_newthings WHERE `user_id`=("+uf.getId()+")" +
				" AND `create_time`<'"+time+"' UNION SELECT `id`,`user_id`,`create_time`,`title`,3 AS 'content_type' FROM ugc_faq WHERE `user_id`=("+uf.getId()+") AND `create_time`<'"+time+"' " +
				"UNION SELECT `id`,`user_id`,`create_time`,`content`,5 AS 'content_type' FROM ugc_faq_answer WHERE `user_id`=("+uf.getId()+") " +
				"AND `create_time`<'"+time+"' UNION SELECT `id`,`user_id`,`create_time`,`content`,4 AS 'content_type' FROM ugc_topic_join WHERE `user_id`=("+uf.getId()+") " +
				"AND `create_time`<'"+time+"' ORDER BY `create_time` DESC LIMIT "+page+",10";
		list=baseDao.Bysql(hql, null);
		return list;
	}


	@SuppressWarnings("unchecked")
	public List<Object> NewThingsImages(Object obj) {//---新鲜事的图片
		List<Object> list=new ArrayList<Object>();
		String hql="select url from ugc_upload_images where from_content_id=("+obj+") and from_content_type=1";
		list=baseDao.Bysql(hql, UgcUploadImages.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object> FaqImage(Object obj) {//---求助的单张图片
		List<Object> list=new ArrayList<Object>();
		String hql="select url from ugc_upload_images where from_content_id=("+obj+") and from_content_type=3 limit 0,1";
		list=baseDao.Bysql(hql, UgcUploadImages.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object> TopicJoinImages(UgcUser u,Object obj) {//--用户参与讨论的图片集合
		List<Object> list=new ArrayList<Object>();
		String hql="select url from ugc_upload_images where user_id="+u.getId()+" and from_content_id=("+obj+") and from_content_type=4";
		list=baseDao.Bysql(hql, UgcUploadImages.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> FaqInf(UgcUser uf, Object obj) {//---查看求助详情
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="select a.id,a.title from ugc_faq as a,ugc_faq_answer as b where a.id=b.to_faq_id and b.id="+obj+" and b.user_id="+uf.getId()+"";
		list=baseDao.Bysql(hql, UgcFaq.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> TopicJoinInf(UgcUser uf,Object obj) {//---话题参与详情
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="SELECT b.id,b.title FROM ugc_topic_join as a,pgc_topic as b where a.to_topic_id=b.id AND a.id="+obj+" and a.user_id="+uf.getId()+"";
		list=baseDao.Bysql(hql, UgcTopicJoin.class);
		return list;
	}

	public Long FaqCountBid(Object obj) {
		String hql="select count(id) from UgcFaqAnswer where toFaqId=("+obj+")";
		return baseDao.CountHql(hql);
	}


	public Long TopicJoinCountBid(Object obj) {//----话题参与个数
		String hql="select count(id) from UgcTopicJoin where toTopicId=("+obj+")";
		return baseDao.CountHql(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> Moment(Object obj, String time, Integer page) {//---首页朋友圈
		List<Object[]> list=new ArrayList<Object[]>();
		String hql="SELECT `id`,`user_id`,`create_time`,`content`,1 AS 'content_type' FROM ugc_newthings WHERE `user_id` IN("+obj+") AND `create_time`<'"+time+"' " +
				"UNION SELECT `id`,`user_id`,`create_time`,`title`,3 AS 'content_type' FROM ugc_faq WHERE `user_id` IN("+obj+") AND `create_time`<'"+time+"' " +
				"UNION SELECT `id`,`user_id`,`create_time`,`content`,4 AS 'content_type' FROM ugc_topic_join WHERE `user_id` IN("+obj+") AND `create_time`<'"+time+"' " +
				"ORDER BY `create_time` DESC LIMIT "+page+",10";
		list=baseDao.Bysql(hql, UgcUser.class);
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> MomentI(Object obj, String time, Integer page) {//---首页朋友圈
		String hql="SELECT `id` id_key,`user_id`,`create_time`,`content` content_text,1 AS 'content_type' FROM ugc_newthings WHERE `user_id` IN("+obj+") AND `create_time`<'"+time+"' " +
				"UNION SELECT `id`,`user_id`,`create_time`,`title`,3 AS 'content_type' FROM ugc_faq WHERE `user_id` IN("+obj+") AND `create_time`<'"+time+"' " +
				"UNION SELECT `id`,`user_id`,`create_time`,`content`,4 AS 'content_type' FROM ugc_topic_join WHERE `user_id` IN("+obj+") AND `create_time`<'"+time+"' " +
				"ORDER BY `create_time` DESC LIMIT "+(page-1)*10+",10";
		return baseDao.Bysql(hql, null);
	}

	public Object UserPartInf(Object obj) {//---查询用户三个信息 返回 ID NickName Photo
		UgcUser u=new UgcUser();
		u.setId(Integer.parseInt(obj.toString()));
		return baseDao.find(UgcUser.class, u.getId());
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> UserThreeInf(Object obj) {
		String hql="select id,nickname,photo,signature from ugc_user where id in("+obj+")";
		return baseDao.Bysql(hql, UgcUser.class);
	}

	public boolean AddUserCollect(UgcCollect uc) {//新建用户收藏
		return baseDao.add(uc);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> UserNewsthingsByLike(Object id_key) {
		String sql="SELECT a.user_id,b.nickname,b.photo FROM ugc_like a,ugc_user b " +
				"WHERE a.user_id=b.id AND  a.to_content_id="+id_key+" AND a.to_content_type=1";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> UserNewthings(UgcUser u,String time,Integer page) {
		String sql="SELECT id id_key,user_id,content,create_time FROM ugc_newthings WHERE user_id=147 and " +
				"create_time<'"+time+"' order by create_time desc LIMIT "+(page-1)*10+",10";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> OneNewthingsInf(UgcNewthings un) {
		String sql="SELECT a.id id_key,a.user_id,a.content,a.create_time,b.nickname,b.photo " +
				"FROM ugc_newthings a,ugc_user b WHERE a.user_id=b.id AND a.id="+un.getId()+"";
		return baseDao.Bysql(sql, null);
	}

	@SuppressWarnings("unchecked")
	public List<Object> MyFriendIds(UgcUser u) {
		String sql="select apply_user_id from ugc_friends a where receive_user_id="+u.getId()+" UNION " +
				"select receive_user_id from ugc_friends r where apply_user_id="+u.getId()+"";
		return baseDao.Bysql(sql, UgcUser.class);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> ListFriendApply(Integer page, UgcUser u) {
		String sql="SELECT a.user_id,COUNT(DISTINCT a.user_id),a.state,a.create_time,b.nickname,b.photo,b.signature FROM ugc_friends_apply a," +
				"ugc_user b WHERE a.user_id=b.id AND to_user_id="+u.getId()+" GROUP BY a.user_id ORDER BY a.create_time DESC LIMIT "+(page-1)*10+",10";
		return baseDao.Bysql(sql, null);
	}

	public List<Map<String, Object>> ListMyQuestionAndAnswer(UgcUser u,
			Integer page, String time) {
		String sql="SELECT id,user_id,create_time,title,1 AS 'content_type' FROM ugc_faq WHERE user_id="+u.getId()+" AND" +
				" create_time<'"+time+"' UNION SELECT id,user_id,create_time,content title,2 as 'content_type' FROM " +
				"ugc_faq_answer WHERE user_id="+u.getId()+" AND create_time<'"+time+"' ORDER BY id DESC limit "+(page-1)*10+",10";
		return baseDao.Bysql(sql, null);
	}

	public List<Object> FindByAnswerId(Object obj) {
		String sql="select to_faq_id from ugc_faq_answer where id="+obj+"";
		return baseDao.Bysql(sql, UgcFaqAnswer.class);
	}

	public List<Object> FindfaqById(Object obj) {
		String sql="select title from ugc_faq where id="+obj+"";
		return baseDao.Bysql(sql, UgcFaq.class);
	}
}
